#include "mycallback.h"


/**
  Constructor.
  */
MyCallback::MyCallback(QObject *parent) :
    Sbs2Callback(parent)
{
    //connect standard signal of spectrogram ready to implemented slot
    connect(sbs2DataHandler,SIGNAL(spectrogramUpdated()),this,SLOT(spectrogramUpdatedSlot()));
}

/**
  Reimplemented callback method. This method sees single packets and handles them.
  */
void MyCallback::getData(Sbs2Packet *packet)
{
    //always set pointer to the current packet
    thisPacket = packet;

    //increase and set counters
    currentPacketCounter =  packet->counter;
    ++ currentPacket;

    //always set the pointer to the packet in sbs2DataHandler
    sbs2DataHandler->setThisPacket(thisPacket);

    //operations to be perfromed
    sbs2DataHandler->filter();
    sbs2DataHandler->spectrogramChannel();

    if (currentPacketCounter%8 == 0)
	emit timeTick8();
}

/**
  Slot called when spectrogram on the channel level is ready.
  */
void MyCallback::spectrogramUpdatedSlot()
{
    //to access power values use sbs2DataHandler->getPowerValues()

    int low = 8;
    int high = 11;

    QList<QVariant> o1;

    //example of extracting channels and frequencies from the spectrogram
    //nicer method for doing that is coming

    for (int row = 0; row < sbs2DataHandler->getPowerValues()->dim1(); ++row)
    {
	if (!(Sbs2Common::getChannelNames()->at(row) == "O1"))
	    continue;

	for (int column = 0; column < sbs2DataHandler->getPowerValues()->dim2(); ++column)
	{
	    if (column < low || column > high)
		continue;

	    if (Sbs2Common::getChannelNames()->at(row) == "O1")
		o1.append((*sbs2DataHandler->getPowerValues())[row][column]);
	}
    }
    emit spectrogramValue("o1",o1);
}
